package com.iiitb.action;

import java.util.Map;
import co.iiitb.DAO.UserDAO;
import com.iiitb.model.User_Login;
import com.iiitb.utils.MyLog;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;


public class Login extends ActionSupport
{

	private String username1;
	private String password;
	private String commandButton;
	private Map<String, Object> session;
	public String getUsername1() {
		return username1;
	}
	public void setUsername1(String username) {
		this.username1 = username;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public String getCommandButton() {
		return commandButton;
	}
	public void setCommandButton(String commandButton) {
		this.commandButton = commandButton;
	}
	
	public Map<String, Object> getSession() {
		return session;
	}
	public void setSession(Map<String, Object> session) {
		this.session = session;
	}
	
	//validates username and password at time of login
	public User_Login validateUseridAndPassword() {
		User_Login user;
		user=UserDAO.findOne(" where username = " + "'"+username1+"'" );
		return user;
	}
	
	
	public String execute() 
	{
		//System.out.println("here");
		MyLog.log("in login execute");
		if (this.username1==null || this.password==null) {
			return "failure";
		}
		User_Login user=validateUseridAndPassword();
		
		if(user!=null)
		{
			session= ActionContext.getContext().getSession();
			session.put("user",user);
			session.put("username",user.getUsername());
			addActionMessage("Welcome");
			return "success";
		}
		else
			addActionError("Authentication Failed.Please try Again");
			return "failure";
	}
	
	
	public String show() 
	{
		MyLog.log("in Login.show().");
		System.out.println("in show:");
		return "success";
		
	}
}
